/**
 * Created by ZFL on 2018/1/3.
 */
    function appInit(){

}

var imgBase='';
var idCardInfo={};    //存放身份证信息

//先判断下是否存在身份证信息   如果存在    渲染在页面上
if($.getStorage('idCardInfo')){
    console.log($.parseJSON($.getStorage('idCardInfo')).idCard)
    $('.idNum input').val($.parseJSON($.getStorage('idCardInfo')).idCard);   //身份证号
    $('.idImg .idCardFront').attr('src',$.parseJSON($.getStorage('idCardInfo')).idCardFront);   //身份证正面
    $('.idImg .idCardReverse').attr('src',$.parseJSON($.getStorage('idCardInfo')).idCardReverse);   //身份证反面
    $('.del').show();
    console.log($.parseJSON($.getStorage('idCardInfo')))
    idCardInfo=$.parseJSON($.getStorage('idCardInfo'))
    isShowBtn()
}

//上传图片
function convertImgToBase64(url, callback, outputFormat){
    var canvas = document.createElement('CANVAS');
    var ctx = canvas.getContext('2d');
    var img = new Image;
    quality=0.6, //image quality
        img.crossOrigin = 'Anonymous';
    img.onload = function(){
        var width = img.width;
        var height = img.height;
        // 按比例压缩6倍
        var rate = (width<height ? width/height:height/width)/6;
        canvas.width = width*rate;
        canvas.height = height*rate;
        ctx.drawImage(img,0,0,width,height,0,0,width*rate,height*rate);

        var dataURL = canvas.toDataURL(outputFormat || 'image/png',quality);
        callback.call(this, dataURL);
        canvas = null;
    };
    img.src = url;
}

function getObjectURL(file) {
    var url = null ;
    if (window.createObjectURL!=undefined) {  // basic
        url = window.createObjectURL(file) ;
    } else if (window.URL!=undefined){       // mozilla(firefox)
        url = window.URL.createObjectURL(file);
    } else if (window.webkitURL!=undefined) { // web_kit or chrome
        url = window.webkitURL.createObjectURL(file) ;
    }
    return url ;
}

$('.uploadImg input').change(function(){
    var _this=this;
    var imageUrl = getObjectURL($(this)[0].files[0]);
    convertImgToBase64(imageUrl, function(base64Img){
        //img_area.innerHTML = '<img src="' + base64Img + '" alt=""/>';
        $(_this).prev('img')[0].src=base64Img;
        // base64转图片不需要base64的前缀data:image/jpg;base64
        // alert(base64Img.split(",")[1]);
        $(_this).siblings('.del').show();
        suffix=base64Img.split(';')[0].split('/')[1];
        //imgObj.suffix=suffix;
        imgBase=base64Img.split(',')[1];
        //imgObj.catalog=2;
        var index=$(_this).parents('.image').index();
        console.log(index)
        uploadIdImgs(index);

    });
})

function uploadIdImgs(index){
    $.ajaxHandler({
        model:function(){
            var obj={};
            obj.catalog=2;
            obj.imgBase=imgBase;
            obj.suffix=suffix;
            console.log(obj)
            return obj;
        },
        visibleTime: 0,
        dataType: "json",
        approve: true,
        api: 'yg_baijiabapp/common/imageBasePath',
        success:function(res){
            if(res.code==10000){
                var data=res.data;
                if(index==0){
                    idCardInfo.idCardFront=data
                }else{
                    idCardInfo.idCardReverse=data;
                }
                isShowBtn();








            }else{
                YDUI.dialog.toast(res.message,1000)
            }
        }
    })
}
//删除按钮
$('.uploadImg .del').click(function(){
        var that=this;
        var index=$(this).parents('.image').index();
        if(index==0){
            idCardInfo.idCardFront=''
        }else{
            idCardInfo.idCardReverse=''
        }
        $(this).siblings('img')[0].src='../../b2c/images/squadron/submit.png';
        $(this).hide();
        console.log(idCardInfo)
        isShowBtn();
})


//判断完成按钮是否展示
function isShowBtn(){
    if(idCardInfo.idCardFront!=undefined&&idCardInfo.idCardReverse!=undefined&&idCardInfo.idCardFront!=''&&idCardInfo.idCardReverse!=''){
        $('.completeBtn').show()
    }else{
        $('.completeBtn').hide()
    }
}


//点击完成   执行跳转
$('.completeBtn').click(function(){
    console.log($('.idNum input').val())
    if($('.idNum input').val()==''){
        YDUI.dialog.toast('请填写身份证号',1000)
    }else{
        idCardInfo.idCard=$('.idNum input').val();

        $.setStorage('idCardInfo',idCardInfo);
        console.log($.parseJSON($.getStorage('idCardInfo')))
        returnGo();    //返回上一页
    }

})
